package pl.cezary.butler.walili.server;

import pl.cezary.butler.walili.common.ILoginFacadeRemote;
import pl.cezary.butler.walili.common.WaliLiSecurityManager;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

// #[regen=yes,id=DCE.74A33C2A-8246-496E-9E63-10F66ADAA6EA]
// </editor-fold> 
public class Server {

    public static final String MAIN_DB = "main";
    private static final Logger log = Logger.getLogger("Serwer");
    private static Registry rejestrRMI;

    static {

        log.setFilter(new Filter() {

            @Override
            public boolean isLoggable(LogRecord arg0) {
                return arg0.getLevel().intValue() < Level.WARNING.intValue();
            }
        });
        initSpringContext();
    }
    private static ApplicationContext context;

    public static synchronized ApplicationContext initSpringContext() {
        if (context == null) {
            try {
                context = new ClassPathXmlApplicationContext(Server.class.getPackage().getName().replace('.', '/') + "/server.xml");
            } catch (BeansException ex) {
                log.log(Level.SEVERE, "Pobranie kontekstu spring nie powiodło się", ex);
                throw new RuntimeException(ex);
            }
        }
        return context;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.343A5A5C-6190-C9B8-8CB8-B84FFCC0D43C]
    // </editor-fold> 
    public Server() {
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.C8EF3A3A-B8A7-559D-F681-3175C09A3A87]
    // </editor-fold> 
    public static void main(String[] args) {

        //MagazynBD.registerBD(MAIN_DB, new BazaDanychMySql());
        try {
            przygotujRejestr();
        } catch (RemoteException ex) {
            log.log(Level.SEVERE, "Problem z przygotowaniem rejestru RMI", ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.62F2191C-9E83-2ADE-4C68-D738E8D451BE]
    // </editor-fold> 
    private static void uruchomWatki() {
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.11A3E506-4336-4F28-052D-186DDF4AEED1]
    // </editor-fold> 
    private static void przygotujRejestr() throws RemoteException {
        try {
            LocateRegistry.createRegistry(WaliLiSecurityManager.PORT);
            rejestrRMI = LocateRegistry.getRegistry(WaliLiSecurityManager.PORT);
            ILoginFacadeRemote login = new LoginFacadeRMI();
            rejestrRMI.bind(login.NAME, UnicastRemoteObject.exportObject(login));
        } catch (AlreadyBoundException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AccessException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Registry getRegistry() {
        return rejestrRMI;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.9848ED75-DF5C-DAB6-00A5-62D4C65CE349]
    // </editor-fold> 
    private static void zarejestrujFasadeLogowania(ILoginFacadeRemote login) {
    }
}
